package com.luyinbros.imagepicker;

import android.content.Context;
import android.graphics.PorterDuff;
import android.support.annotation.Nullable;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.bumptech.glide.Glide;
import com.luyinbros.imagepicker.model.LocalMedia;

import java.io.File;
import java.util.List;

/**
 * Description:
 * Created by Andy Hong on 6/25/2017.
 */

class ImagePickerAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {
    private  final int TYPE_CAMERA = 1;
    private  final int TYPE_PICTURE = 2;
    private LayoutInflater mInflater;
    private boolean showCamera;
    private ImageDataHelper mHelper;
    private OnImageSelectedListener mListener;
    private Context context;

    public ImagePickerAdapter(Context context, boolean showCamera, int maxSelectNum) {
        this.context = context;
        mInflater = LayoutInflater.from(context);
        this.showCamera = showCamera;
        mHelper = new ImageDataHelper(maxSelectNum);
    }

    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        if (viewType == TYPE_CAMERA) {
            return new CameraHolder(mInflater.inflate(R.layout.item_camera, parent, false));
        }
        return new ItemHolder(mInflater.inflate(R.layout.item_picture, parent, false));
    }

    public void setOnImageSelectedClickListener(OnImageSelectedListener listener) {
        this.mListener = listener;
    }

    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) {
        if (holder instanceof CameraHolder) {
            holder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.takePhoto();
                    }
                }
            });
            ((CameraHolder) holder).listener = mListener;
        } else if (holder instanceof ItemHolder) {
            final ItemHolder itemHolder = (ItemHolder) holder;
            final int picPosition = showCamera ? position - 1 : position;
            final LocalMedia data = mHelper.getItem(picPosition);

            Glide.with(context)
                    .load(new File(data.getPath()))
                    .centerCrop()
                    .thumbnail(0.5f)
                    .placeholder(R.drawable.ic_placeholder)
                    .error(R.drawable.ic_placeholder)
                    .dontAnimate()
                    .into(itemHolder.ivPicture);
            itemHolder.itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (mListener != null) {
                        mListener.onPictureClick(picPosition);
                    }
                }
            });
            if (mHelper.getMaxSelectNum()==1){
                itemHolder.ivCheck.setVisibility(View.GONE);
            }else{
                itemHolder.ivCheck.setSelected(mHelper.isSelected(data));
                itemHolder.ivPicture.setColorFilter(itemHolder.ivCheck.isSelected() ? 0x80000000 : 0x20000000, PorterDuff.Mode.SRC_ATOP);
                itemHolder.ivCheck.setOnClickListener(new View.OnClickListener() {
                    @Override
                    public void onClick(View v) {
                        boolean isSelected = !v.isSelected();
                        if (isSelected) {
                            if (mHelper.isMaxSelect()) {
                                Context context = v.getContext();
                                Toast.makeText(context, String.format(context.getString(R.string.message_max_num), mHelper.getMaxSelectNum()), Toast.LENGTH_SHORT).show();
                            } else {
                                mHelper.select(data);
                                v.setSelected(true);
                                itemHolder.ivPicture.setColorFilter(0x80000000, PorterDuff.Mode.SRC_ATOP);
                                if (mListener != null) {
                                    mListener.onSelected(mHelper.getSelectList());
                                }
                            }
                        } else {
                            v.setSelected(false);
                            mHelper.removeSelect(data);
                            itemHolder.ivPicture.setColorFilter(0x20000000, PorterDuff.Mode.SRC_ATOP);
                            if (mListener != null) {
                                mListener.onSelected(mHelper.getSelectList());
                            }
                        }
                    }
                });
            }

        }
    }


    public List<LocalMedia> getSelectList() {
        return mHelper.getSelectList();
    }

    public List<LocalMedia> getAllList() {
        return mHelper.getAllList();
    }

    public LocalMedia getItem(int position) {
        return mHelper.getItem(position);
    }

    public void updateList(@Nullable List<LocalMedia> source) {
        mHelper.setList(source);
        notifyDataSetChanged();
    }

    public void updateSelectList(@Nullable List<LocalMedia> source) {
        mHelper.setSelectList(source);
        notifyDataSetChanged();
    }

    @Override
    public int getItemCount() {
        return showCamera ? mHelper.allSize() + 1 : mHelper.allSize();
    }

    @Override
    public int getItemViewType(int position) {
        if (showCamera && position == 0) {
            return TYPE_CAMERA;
        }
        return TYPE_PICTURE;
    }

    private static class CameraHolder extends RecyclerView.ViewHolder {
        private OnImageSelectedListener listener;

        public CameraHolder(View itemView) {
            super(itemView);
            itemView.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
                    if (listener != null) {
                        listener.takePhoto();
                    }
                }
            });
        }
    }

    private static class ItemHolder extends RecyclerView.ViewHolder {
        private ImageView ivCheck;
        private ImageView ivPicture;

        public ItemHolder(View itemView) {
            super(itemView);
            ivCheck = (ImageView) itemView.findViewById(R.id.check);
            ivPicture = (ImageView) itemView.findViewById(R.id.picture);
        }

    }

    public interface OnImageSelectedListener {

        void takePhoto();

        void onPictureClick(int position);

        void onSelected(List<LocalMedia> localMediaList);
    }


}
